package me.send.produce.data.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import me.send.business.base.Pager;
import me.send.data.service.pojo.VersionBean;
import me.send.data.service.pojo.base.DatatablesViewPage;
import me.send.data.service.service.TreeService;
import me.send.data.service.service.VersionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "version")
public class VersionController {

	@Autowired
	private VersionService versionService;

	@Autowired
	private TreeService treeService;


	// 查询提现订单列表
	@RequestMapping(value = "list")
	public ModelAndView version_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/version/version_list");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "version/list"));
		modelAndView.addObject("module", "版本列表");
		return modelAndView;
	}

	
	@RequestMapping(value="page", method=RequestMethod.GET)  
    @ResponseBody  
    public DatatablesViewPage<VersionBean> version_page(HttpServletRequest request){  
//获取分页控件的信息  
        String start = request.getParameter("start");  
        String length = request.getParameter("length"); 
//获取前台额外传递过来的查询条件  
        String type = request.getParameter("type");  
        Integer paramType=null;
        if(type==null||(type!=null&&"".equals(type.trim()))){
        	type=null;
        }else{
        	paramType=Integer.parseInt(type);
        }
        
        Pager page=new Pager();
        Integer row=Integer.parseInt(length);
        page.setRows(row);
        Integer startInt=Integer.parseInt(start);
        Integer p=(startInt/row)+1;
        page.setPage(p);
        
                //随便组织的查询结果  
        List<VersionBean> list=versionService.list(paramType,page); 
        Integer count=versionService.count(paramType);
        DatatablesViewPage<VersionBean> view = new DatatablesViewPage<VersionBean>();  
        view.setiTotalDisplayRecords(count);  
        view.setiTotalRecords(count);  
          
        view.setAaData(list);  
        return view;  
    }  
	
	@RequestMapping(value = "edit/detail")
	public ModelAndView recharge_edit_detail(HttpServletRequest request,int version_id) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/version/version_edit_detail");
		VersionBean version=versionService.queryVersionById(version_id);
				
		modelAndView.addObject("version",version);
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "version/edit/detail"));
		modelAndView.addObject("module", "版本编辑");
		return modelAndView;
	}
	
	/**
	 * 修改
	 * @param listId
	 * @return
	 */
	@RequestMapping(value = "edit")
	@ResponseBody
	public ModelAndView update(HttpServletRequest request) {
		Long version_id=Long.parseLong(request.getParameter("version_id"));
		String version_code=request.getParameter("version_code");
		String description=request.getParameter("description");
		String url=request.getParameter("url");
		Integer type=Integer.parseInt(request.getParameter("type"));
		VersionBean version=new VersionBean();
		version.setDescription(description);
		version.setType(type);
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		version.setUpdatetime(sdf.format(new Date()));
		version.setUrl(url);
		version.setVersion_code(version_code);
		version.setVersion_id(version_id.intValue());
		versionService.update(version);
		ModelAndView modelAndView = new ModelAndView(
				"stage/version/version_list");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "version/list"));
		modelAndView.addObject("module", "版本列表");
		return modelAndView;
	}
	
}





